package com.difsonapps.contacttranslator.component;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.difsonapps.contacttranslator.dto.MessageDTO;
import com.difsonapps.contacttranslator.utils.CommonProps;
import com.sun.mail.smtp.SMTPMessage;


@Component
public class MailService {

	Logger log = Logger.getLogger(this.getClass());

	public void sendMailWithImage(MessageDTO messageDTO, String content,
			CommonProps commonProps) throws Exception {
		Properties props = new Properties();
		props.setProperty("mail.smtp.host", commonProps.mailHost);
		props.setProperty("mail.smtp.port", commonProps.mailPort);
		props.setProperty("mail.smtp.starttls.enable", commonProps.mailStarttls);
		props.setProperty("mail.smtp.auth", commonProps.mailAuth);

		final String username = commonProps.mailUsername;
		final String password = commonProps.mailPassword;
		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});

		SMTPMessage m = new SMTPMessage(session);
		MimeMultipart mimeMultipart = new MimeMultipart("related");

		String cid1 = "image1";
		String cid2 = "image2";

		// HTML part
		MimeBodyPart textPart = new MimeBodyPart();
		textPart.setText(content, 
			      "US-ASCII", "html");
		mimeMultipart.addBodyPart(textPart);

		// Image part
		MimeBodyPart imagePart = new MimeBodyPart();
		imagePart.attachFile(commonProps.cover);
		imagePart.setContentID("<" + cid2 + ">");
		imagePart.setDisposition(MimeBodyPart.INLINE);
		mimeMultipart.addBodyPart(imagePart);

		MimeBodyPart imagePart2 = new MimeBodyPart();
		imagePart2.attachFile(commonProps.logo);
		imagePart2.setContentID("<" + cid1 + ">");
		imagePart2.setDisposition(MimeBodyPart.INLINE);
		mimeMultipart.addBodyPart(imagePart2);

		m.setContent(mimeMultipart);
		m.setSubject(messageDTO.getSubject());

		// Set From: header field of the header.
		m.setFrom(new InternetAddress(messageDTO.getFrom(),messageDTO.getFromDisplayName()));

		// Set To: header field of the header.
		m.addRecipient(Message.RecipientType.TO, new InternetAddress(
				messageDTO.getTo()));
		// Set To: header field of the header.
		if (messageDTO.getCc() != null) {
			m.addRecipient(Message.RecipientType.CC,
					new InternetAddress(messageDTO.getCc()));
		}
		// Set To: header field of the header.
		if (messageDTO.getBcc() != null) {
			m.addRecipient(Message.RecipientType.BCC,
					new InternetAddress(messageDTO.getBcc()));
		}
		Transport.send(m);
		log.info("Sent message successfully....");

	}
	public void sendMail(MessageDTO messageDTO, String content,
			CommonProps commonProps) throws Exception {
		Properties props = new Properties();
		log.info("commonProps.mailHost="+commonProps.mailHost+" commonProps.mailPort="+commonProps.mailPort
				+" commonProps.mailUsername="+commonProps.mailUsername+"  commonProps.mailPassword="+ commonProps.mailPassword);
		log.info(content);
		props.setProperty("mail.smtp.host", commonProps.mailHost);
		props.setProperty("mail.smtp.port", commonProps.mailPort);
		props.setProperty("mail.smtp.starttls.enable", commonProps.mailStarttls);
		props.setProperty("mail.smtp.auth", commonProps.mailAuth);

		final String username = commonProps.mailUsername;
		final String password = commonProps.mailPassword;
		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});
		try {
			// Create a default MimeMessage object.
			MimeMessage message = new MimeMessage(session);

			// Set From: header field of the header.
			message.setFrom(new InternetAddress(messageDTO.getFrom(),messageDTO.getFromDisplayName()));

			// Set To: header field of the header.
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					messageDTO.getTo()));

			// Set To: header field of the header.
			if (messageDTO.getCc() != null) {
				message.addRecipient(Message.RecipientType.CC,
						new InternetAddress(messageDTO.getCc()));
			}
			// Set To: header field of the header.
			if (messageDTO.getBcc() != null) {
				message.addRecipient(Message.RecipientType.BCC,
						new InternetAddress(messageDTO.getBcc()));
			}

			// Set Subject: header field
			message.setSubject(messageDTO.getSubject());

			// Send the actual HTML message, as big as you like
			message.setContent(content, "text/html");

			// Send message
			Transport.send(message);
			log.info("Sent message successfully....");
		} catch (MessagingException mex) {
			throw new Exception(mex.getLocalizedMessage(), mex);
		}

	}
}
